package com.azkj.album.presenter;

import static com.luck.picture.lib.utils.PictureFileUtils.getPath;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;

import com.azkj.album.MyApplication;
import com.azkj.album.dto.AddOrderBean;
import com.azkj.album.dto.BackGroundListBean;
import com.azkj.album.dto.ListData;
import com.azkj.album.dto.NewGalleryBean;
import com.azkj.album.dto.NewPhotoBean;
import com.azkj.album.dto.UploadBean;
import com.azkj.album.dto.VipInfoBean;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.callback.UploadCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.view.base.BasePresenter;
import com.azkj.album.view.iview.INewPhotoView;
import com.azkj.album.view.widgets.dialog.DialogHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class NewPhotoPresenter extends BasePresenter {

    private final INewPhotoView iView;

    public NewPhotoPresenter(INewPhotoView iView) {
        this.iView = iView;
    }

    public void classPhoto() {
        NetworkMaster.getInstance().getCommonService().getClassPhoto(new ServiceCallback<BaseResp<List<NewPhotoBean>>>() {
            @Override
            public void onSuccess(BaseResp<List<NewPhotoBean>> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.classSuccess(jsonObj.getData());
                } else {
                    iView.classFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.classFail(apiException.errorInfo.error);
            }
        });
    }

    public void UploadFile(String token, NewGalleryBean bean, int position) {
        UploadFile(token, bean, position, false, -1);
    }

    public void UploadBitmap(NewGalleryBean bean, int position) {
        NetworkMaster.getInstance().getCommonService().uploadBitmap(bean.getBitmap(), new UploadCallback<BaseResp<UploadBean>>() {
            @Override
            public void onSuccess(BaseResp<UploadBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    UploadBean uploadBean = jsonObj.getData();
                    bean.setFullUrl(uploadBean.getFullurl());
                    iView.uploadFileSuccess(position, bean, false, -1);
                } else {
                    iView.uploadFileFail(jsonObj.getMsg());
                }
                if (bean.getBitmap() != null) {
                    bean.getBitmap().recycle();
                }
            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iView.uploadFileFail(apiException.errorInfo.error);
                if (bean.getBitmap() != null) {
                    bean.getBitmap().recycle();
                }
            }

            @Override
            public void onProgress(long currentLength, long contentLength) {
                int progress = (int) ((currentLength / (double) contentLength) * 100);
                if (bean.getProgress() != progress) {
                    bean.setProgress(progress);
                    iView.uploadProcess(position, bean, false);
                }
            }
        });
    }

    //上传文件
    public void UploadFile(String token, NewGalleryBean bean, int position, boolean isCompile, int saveCount) {
        File file = new File(bean.getImgPath());

        Luban.with(MyApplication.getApplication())
                .load(file)
                .ignoreBy(100)
                .filter(path -> !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif")))
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        Log.d("NewPhotoPresenter", "onStart: ");
                    }

                    @Override
                    public void onSuccess(File file) {
                        Log.d("NewPhotoPresenter", "onSuccess: file" + file.getAbsolutePath());
                        NetworkMaster.getInstance().getCommonService().getUploadFile(token, file, new UploadCallback<BaseResp<UploadBean>>() {
                            @Override
                            public void onSuccess(BaseResp<UploadBean> jsonObj) {
                                DialogHelper.hideDialog();
                                if (jsonObj.getCode() == 1) {
                                    UploadBean uploadBean = jsonObj.getData();
                                    bean.setFullUrl(uploadBean.getFullurl());
                                    iView.uploadFileSuccess(position, bean, isCompile, saveCount);
                                } else {
                                    iView.uploadFileFail(jsonObj.getMsg());
                                }
                            }

                            @Override
                            public void onError(ApiException apiException) {
                                DialogHelper.hideDialog();
                                iView.uploadFileFail(apiException.errorInfo.error);
                            }

                            @Override
                            public void onProgress(long currentLength, long contentLength) {
                                int progress = (int) ((currentLength / (double) contentLength) * 100);
                                if (bean.getProgress() != progress) {
                                    bean.setProgress(progress);
                                    iView.uploadProcess(position, bean, isCompile);
                                }
                            }
                        });

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("NewPhotoPresenter", "onError: ");
                    }
                }).launch();
    }

    /**
     * token token,
     * id id=0新增否则是编辑,
     * category_id	分类ID,
     * background_id  背景图ID,
     * background_image 背景图,
     * map_id 	贴图ID,
     * map_image  贴图,
     * images 	相册图片，多张逗号分割
     * 生成封面的图片，多张逗号分割
     * intro 简介,
     * cover_image 封面,
     * remark 备注.
     */
    public void newSave(String token, String id, String category_id, String background_id, String background_image, String map_id, String map_image, String images,
                        String choose_images, String choose_image, String intro, String cover_image, String remark) {
        NetworkMaster.getInstance().getCommonService().postNewSave(token, id, category_id, background_id, background_image, map_id, map_image, images,
                choose_images, choose_image, intro, cover_image, remark, new ServiceCallback<BaseResp<String>>() {
                    @Override
                    public void onSuccess(BaseResp<String> jsonObj) {
                        DialogHelper.hideDialog();
                        if (jsonObj.getCode() == 1) {
                            iView.newSaveSuccess(jsonObj.getData());
                        } else {
                            iView.newSaveFail(jsonObj.getMsg());
                        }
                    }

                    @Override
                    public void onError(ApiException apiException) {
                        iView.newSaveFail(apiException.errorInfo.error);
                    }
                });
    }

    /**
     * token token,
     * id id=0新增否则是编辑,
     * category_id	分类ID,
     * background_id  背景图ID,
     * background_image 背景图,
     * map_id 	贴图ID,
     * map_image  贴图,
     * images 	相册图片，多张逗号分割
     * choose_images 生成封面的图片，多张逗号分割
     * intro 简介,
     * cover_image 封面,
     * remark 备注.
     */
    public void newSaveAlbum(String token, String id, String category_id, String background_id, String background_image, String map_id, String map_image, String images, String choose_images, String intro, String cover_image, String remark) {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("id", id);
        map.put("category_id", category_id);
        map.put("background_id", background_id);
        map.put("background_image", background_image);
        map.put("map_id", map_id);
        map.put("map_image", map_image);
        map.put("images", images);
        map.put("choose_images", choose_images);
        map.put("intro", intro);
        map.put("cover_image", cover_image);
        map.put("remark", remark);
        NetworkMaster.getInstance().getCommonService().saveAlbum(map, new ServiceCallback<BaseResp<Object>>() {
            @Override
            public void onSuccess(BaseResp<Object> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.newSaveSuccess(jsonObj.getData().toString());
                } else {
                    iView.newSaveFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.newSaveFail(apiException.errorInfo.error);
            }
        });
    }

    public void getPreDetail() {
        NetworkMaster.getInstance().getCommonService().preDetail(new ServiceCallback<BaseResp<ListData>>() {

            @Override
            public void onSuccess(BaseResp<ListData> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.getPerDetail(jsonObj.getData());
                } else {
                    iView.getPerDetailFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.getPerDetailFail(apiException.errorInfo.error);
            }
        });
    }

    //背景分类
    public void selectBackClass() {
        NetworkMaster.getInstance().getCommonService().getSelectBackClass(new ServiceCallback<BaseResp<List<NewPhotoBean>>>() {
            @Override
            public void onSuccess(BaseResp<List<NewPhotoBean>> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.backGroundSuccess(jsonObj.getData());
                } else {
                    iView.backGroundFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.backGroundFail(apiException.errorInfo.error);
            }
        });
    }

    //背景列表
    public void selectBackList(String category_id, String page, String limit) {
        NetworkMaster.getInstance().getCommonService().getSelectBackList(category_id, page, limit, new ServiceCallback<BaseResp<List<BackGroundListBean>>>() {
            @Override
            public void onSuccess(BaseResp<List<BackGroundListBean>> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.backGroundListSuccess(jsonObj.getData());
                } else {
                    iView.backGroundListFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.backGroundListFail(apiException.errorInfo.error);
            }
        });
    }

    //历史列表
    public void selectIntro(String page, String limit, String keyword) {
        NetworkMaster.getInstance().getCommonService().getSelectIntro(page, limit, keyword, new ServiceCallback<BaseResp<List<String>>>() {
            @Override
            public void onSuccess(BaseResp<List<String>> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.introSuccess(jsonObj.getData());
                } else {
                    iView.introFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.introFail(apiException.errorInfo.error);
            }
        });
    }

    //备注列表
    public void selectRemark(String page, String limit, String keyword) {
        NetworkMaster.getInstance().getCommonService().getSelectRemark(page, limit, keyword, new ServiceCallback<BaseResp<List<String>>>() {
            @Override
            public void onSuccess(BaseResp<List<String>> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.remarkSuccess(jsonObj.getData());
                } else {
                    iView.remarkFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.remarkFail(apiException.errorInfo.error);
            }
        });
    }

    //新建任务判断
    public void canAddOrder() {
        NetworkMaster.getInstance().getCommonService().getCanAddOrder(new ServiceCallback<BaseResp<AddOrderBean>>() {
            @Override
            public void onSuccess(BaseResp<AddOrderBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.addOrderSuccess(jsonObj.getData());
                } else {
                    iView.addOrderFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.addOrderFail(apiException.errorInfo.error);
            }
        });
    }

    public void getVip() {
        NetworkMaster.getInstance().getCommonService().getVipInfo(new ServiceCallback<BaseResp<VipInfoBean>>() {
            @Override
            public void onSuccess(BaseResp<VipInfoBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.VipSuccess(jsonObj.getData());
                } else {
                    iView.VipFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iView.VipFail(apiException.errorInfo.error);
            }
        });
    }

    //删除历史简介
    public void deleteHistoryorIntro(String content) {
        NetworkMaster.getInstance().getCommonService().getDeleteIntro(content, new ServiceCallback<BaseResp<String>>() {
            @Override
            public void onSuccess(BaseResp<String> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.deleteIntroSuccess(jsonObj.getData());
                } else {
                    iView.deleteIntroFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.deleteIntroFail(apiException.errorInfo.error);
            }
        });
    }

    //删除历史备注
    public void deleteHistoryorRemake(String content) {
        NetworkMaster.getInstance().getCommonService().getDeleteRemark(content, new ServiceCallback<BaseResp<String>>() {
            @Override
            public void onSuccess(BaseResp<String> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iView.deleteRemarkSuccess(jsonObj.getData());
                } else {
                    iView.deleteRemarkFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iView.deleteRemarkFail(apiException.errorInfo.error);
            }
        });
    }
}
